﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace LicenseData
{
	public class LicenseDetails : IEquatable<LicenseDetails>
	{
		public const int ZipCodeIndex = 18;
		public const int CallsignIndex = 4;
		public const int NameIndex = 8;
		public const int LocationIndex = 15;

		public string RosterId
		{
			get;
			private set;
		}

		public string Name
		{
			get;
			private set;
		}

		public string Location
		{
			get;
			private set;
		}

		public string ZipCode
		{
			get;
			private set;
		}

		public DateTimeOffset? UpdateTime
		{
			get;
			private set;
		}

		public bool IsRemoved
		{
			get;
			private set;
		}

		public string LocationSqlValue
		{
			get
			{
				if ( string.IsNullOrWhiteSpace( Location ) )
					return "NULL";
				else
					return string.Format( "'{0}'", Location );
			}
		}

		public string ZipCodeSqlValue
		{
			get
			{
				if ( string.IsNullOrWhiteSpace( ZipCode ) )
					return "NULL";
				else
					return string.Format( "'{0}'", ZipCode );
			}
		}

		public LicenseDetails( string[] values )
		{
			RosterId = values[ CallsignIndex ];
			Name = values[ NameIndex ];
			Location = values[ LocationIndex ];
			ZipCode = values[ ZipCodeIndex ].Substring( 0, 5 );
		}

		public LicenseDetails( string values )
		{
			string[]
				splitValues = values.Split( '\t' );

			RosterId = splitValues[ 0 ];
			Name = splitValues[ 1 ];
			Location = splitValues[ 2 ];
			ZipCode = splitValues[ 3 ];
		}

		public LicenseDetails( XElement node )
		{
			foreach ( XElement item in node.Elements() )
			{
				switch ( item.Name.LocalName )
				{
					case "roster_id":
						RosterId = ConversionHelpers.DecodedString( item );
						break;
					case "name":
						Name = ConversionHelpers.DecodedString( item );
						break;
					case "location":
						Location = ConversionHelpers.DecodedString( item );
						break;
					case "zip_code":
						ZipCode = ConversionHelpers.DecodedString( item );
						break;
					case "is_removed":
						IsRemoved = ConversionHelpers.ToBoolean( item );
						break;
					case "update_time":
						UpdateTime = ConversionHelpers.FromMySQLDate( item );
						break;
				}
			}
		}

		public override string ToString()
		{
			return string.Format( "{0}\t{1}\t{2}\t{3}", RosterId, Name, Location, ZipCode );
		}

		public int WriteLicenseUpdateSql( StreamWriter writer, DateTimeOffset updateTime, bool isRemoved )
		{
			writer.WriteLine( "INSERT INTO license_updates ( roster_id, name, location, zip_code, update_time, is_removed ) VALUES ( '{0}', '{1}', {2}, {3}, '{4}', {5} );",
				RosterId, Name, LocationSqlValue, ZipCodeSqlValue, ConversionHelpers.ToMySQLDate( updateTime.ToUniversalTime() ),
				ConversionHelpers.FromBoolean( isRemoved ) );
			writer.WriteLine( "DELETE FROM former_members WHERE roster_id = '{0}';", RosterId );
			return 2;
		}

		public bool Equals( LicenseDetails other )
		{
			return RosterId == other.RosterId && Name == other.Name && LocationSqlValue == other.LocationSqlValue && ZipCodeSqlValue == other.ZipCodeSqlValue;
		}
	}
}
